home *** CD-ROM | disk | FTP | other *** search
- Imports System.Data.OleDb
-
- ' this page demonstrates the AutoPostBack feature of the
- ' DropDownList control
-
- Public Class WebForm2
- Inherits System.Web.UI.Page
- Protected WithEvents ddlStates As System.Web.UI.WebControls.DropDownList
- Protected WithEvents dgPublishers As System.Web.UI.WebControls.DataGrid
- Protected WithEvents lblCount As System.Web.UI.WebControls.Label
- Protected WithEvents Label1 As System.Web.UI.WebControls.Label
-
- #Region " Web Form Designer Generated Code "
-
- 'This call is required by the Web Form Designer.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
-
- End Sub
-
- Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
- 'CODEGEN: This method call is required by the Web Form Designer
- 'Do not modify it using the code editor.
- InitializeComponent()
- End Sub
-
- #End Region
-
- Dim cn As New OleDbConnection(BiblioConnString)
-
- Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- ' fill the state list only the first time the page is requested
- If Not Page.IsPostBack Then
- InitializeStateList()
- End If
-
- ' Number of requests to this page posted so far.
- Dim count As Integer
- If Not Me.ViewState("count") Is Nothing Then
- count = CInt(Me.ViewState("count")) + 1
- End If
- ' Store the value back in the ViewState dictionary.
- Me.ViewState("count") = count
- ' Display in a Label control.
- lblCount.Text = count.ToString
- End Sub
-
- ' Display unique state strings in the ddlStates DropDownList control.
-
- Sub InitializeStateList()
- cn.Open()
- Dim cmd As New OleDbCommand("SELECT DISTINCT State FROM Publishers", cn)
- Dim dr As OleDbDataReader = cmd.ExecuteReader()
- ddlStates.DataSource = dr
- ' The field used for filling the list are of the DropDownList control.
- ddlStates.DataTextField = "State"
- ddlStates.DataBind()
- ' close the DataReader and the Connection
- dr.Close()
- cn.Close()
- End Sub
-
- ' display all states or only a subset of all states in the DataGrid
- ' depending on which element is selected in the DropDownList
-
- Private Sub ShowPublishers()
- ' Create the query string.
- Dim sql As String = "SELECT Name, Address, City, State FROM Publishers"
- If ddlStates.SelectedIndex >= 0 Then
- sql &= " WHERE State = '" & ddlStates.SelectedItem.Text & "'"
- End If
-
- ' Display All publishers in the DataGrid.
- If cn.State <> ConnectionState.Open Then cn.Open()
- Dim cmd As New OleDbCommand(sql, cn)
- Dim dr As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
- dgPublishers.DataSource = dr
- dgPublishers.DataBind()
- dr.Close()
- End Sub
-
- ' when a new state is selected in the DropDownList control, it
- ' causes a postback and this routine fires
-
- Private Sub ddlStates_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlStates.SelectedIndexChanged
- ShowPublishers()
- End Sub
-
- End Class
-